package com.typy.www.tpay;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.android.volley.VolleyError;
import com.typy.www.tpay.base.BaseActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.vm.LoginVm;
import com.typy.www.tpay.bean.vm.UserInfoVm;
import com.typy.www.tpay.bean.vm.VersionVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.constance.Sharep;
import com.typy.www.tpay.ui.acitivity.HomeActivity;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PreferencesUtil;
import com.typy.www.tpay.util.SharedPreferencesUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.ToastUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/7.
 */

public class WelComeActivity extends BaseActivity {


    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    private int status;

    @Override
    protected int getTitleBarResId() {
        return -1;
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_linear;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        llRoot.setBackgroundResource(R.drawable.start);
        checkVersion();

    }
    private void checkToken(){
        if (!StringUtil.isEmpty( PreferencesUtil.getValue( Sharep.PASSWORD))) {
            getData();
        } else {
            LoginActivity.show(this);
            finish();
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (status == 0) {
                    status++;
                } else {
                    MainActivity.show(WelComeActivity.this);
                    finish();
                }
            }
        }, 2000);
    }
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent msg){
        if(msg.getKey().equals(MessageAction.CLOSEWELCOME)){
            finish();
        }
    }
    //验证token
    private void getData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(getActivity(), Constance.getMyselfinfoURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                HomeActivity.show(WelComeActivity.this);
                finish();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                LoginActivity.show(getActivity());
                finish();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                super.onErrorResponse(error);
                LoginActivity.show(getActivity());
                finish();
            }
        });
    }

    private void checkVersion(){
        ParamsUtil params = new ParamsUtil();
        params.put("fdType","android");
        HTTPUtils.post(getActivity(), Constance.getCheckversionURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if(ret!=null&&ret.getData()!=null){
                    final VersionVm vm=GsonUtils.parseJSON(ret.getData().toString(),VersionVm.class);
                    int version=StringUtil.getLocalVersionCord(getActivity());
                    if(vm.getFdVersion()<=(version)){
                        checkToken();
                    }else{
                        SyDialogHelper.showDlg(getActivity(), "提示", "当前版本不是最新版本，前往更新", "确定", new SyMessageDialog.OnClickListener() {
                            @Override
                            public void onClick(SyMessageDialog dialog) {
                                Uri uri = Uri.parse(vm.getFdDownUrl());
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                startActivity(intent);
                            }
                        });
                    }
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
//                checkToken();
                LoginActivity.show(getActivity());
                finish();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                super.onErrorResponse(error);
                LoginActivity.show(getActivity());
                finish();
            }
        });
    }

}
